home *** CD-ROM | disk | FTP | other *** search
- package gratest;
-
- import javax.microedition.lcdui.Command;
- import javax.microedition.lcdui.CommandListener;
- import javax.microedition.lcdui.Display;
- import javax.microedition.lcdui.Displayable;
- import javax.microedition.lcdui.Form;
- import javax.microedition.lcdui.TextField;
- import javax.microedition.midlet.MIDlet;
- import javax.microedition.midlet.MIDletStateChangeException;
-
- public class Tetris extends MIDlet implements CommandListener {
- private Command exitCommand = new Command("Exit", 7, 2);
- private Command newGameCommand = new Command("New", 4, 2);
- private Command returnCommand = new Command("Menu", 4, 2);
- private Display display = Display.getDisplay(this);
- private TetrisCanvas screen;
- private Form form = new Form("Tetris");
- private TextField boxsize = new TextField("Number of boxes:", "12", 2, 2);
- private TextField level = new TextField("Starting level(1-9):", "3", 1, 2);
-
- public Tetris() {
- this.form.append(this.boxsize);
- this.form.append(this.level);
- this.form.addCommand(this.exitCommand);
- this.form.addCommand(this.newGameCommand);
- this.form.setCommandListener(this);
- }
-
- public void startApp() throws MIDletStateChangeException {
- this.display.setCurrent(this.form);
- }
-
- public void pauseApp() {
- }
-
- public void destroyApp(boolean unconditional) {
- this.screen.tetTimer.cancel();
- }
-
- public void commandAction(Command c, Displayable s) {
- if (c == this.exitCommand) {
- this.destroyApp(false);
- ((MIDlet)this).notifyDestroyed();
- } else if (c == this.newGameCommand) {
- int iNum = Integer.parseInt(this.boxsize.getString());
- if (iNum > 20 || iNum < 10) {
- iNum = 12;
- }
-
- int iLevel = Integer.parseInt(this.level.getString());
- if (iLevel > 9 || iLevel < 1) {
- iLevel = 1;
- }
-
- this.screen = new TetrisCanvas(iNum, iLevel);
- this.screen.addCommand(this.exitCommand);
- this.screen.addCommand(this.returnCommand);
- this.screen.setCommandListener(this);
- this.display.setCurrent(this.screen);
- } else if (c == this.returnCommand) {
- this.screen.tetTimer.cancel();
- this.screen = null;
- this.display.setCurrent(this.form);
- }
-
- }
- }
-